// form validation function //
function validate(form) {

    var title = form.title.value;
	var author = form.author.value;
	var publisher = form.publisher.value;
	var price = form.price.value;
	var isbn = form.isbn.value;
	var image = form.image.value;
	var file = form.file.value;
	var pricechk = /^[0-9]{1,5}$/;
	var isbnchk = /^[0-9]{13}$/;
	var namechk = /^[A-Za-z]{3,25}$/;
	var pblchk = /^[A-Za-z]{1}[A-Za-z0-9]{2,25}$/;
	/*
  var cno = /^[7-9]{1}[0-9]{9}$/;
  var c_password = form.c_password.value;
  var name = form.name.value;
  var namechk = /^[A-Za-z]{3,25}$/;
  var contact = form.contact.value;
 var address = form.address.value; 
 var city = form.city.value;
 var citychk = /^[A-Za-z]{3,16}$/;
 var state = form.state.value;
 var statechk = /^[A-Za-z]{3,16}$/;
 var pincode = form.pincode.value;
 var pinchk = /^[0-9]{6}$/;
var country = form.country.value;
var answer = form.answer.value;
var captcha = form.captcha.value; */

  /*var security_question = form.security_question.value;
   var security_answer = form.security_answer.value;
   var captcha_ans = form.captcha_ans.value;
   var var1 = form.var1.value;
   var var2 = form.var2.value;
   var var3 = parseInt(var1) + parseInt(var2);
  */
 if(title == "") {
    inlineMsg('title','<strong>Error</strong><br />You must have to enter title.',2);
    return false;
  }
if(author == "") {
    inlineMsg('author','<strong>Error</strong><br />You must have to enter author.',2);
    return false;
  }
  if(!author.match(namechk)) {
    inlineMsg('author','<strong>Error</strong><br />You must enter a minimun 3 character name. NO numbers are allowed',2);
    return false;
  }
  if(publisher == "") {
    inlineMsg('publisher','<strong>Error</strong><br />You must have to enter Publisher.',2);
    return false;
  }
  if(!publisher.match(pblchk)) {
    inlineMsg('publisher','<strong>Error</strong><br />You must enter a minimun 3 character name. Only Numbers are not allowded',2);
    return false;
  }
  if(price == "") {
    inlineMsg('price','<strong>Error</strong><br />You must have to enter Price.',2);
    return false;
  }
  if(!price.match(pricechk)) {
    inlineMsg('price','<strong>Error</strong><br/>Price Must be only in Digits.',2);
    return false;
  }
  if(isbn == "") {
    inlineMsg('isbn','<strong>Error</strong><br />You must have to enter ISBN.',2);
    return false;
  }
  if(!isbn.match(isbnchk)) {
    inlineMsg('isbn','<strong>Error</strong><br/>ISBN Must be only 13 Digit Number.',2);
    return false;
  }
  if(image == "") {
    inlineMsg('image','<strong>Error</strong><br />You must have to Upload Image',2);
    return false;
  }
  if(file == "") {
    inlineMsg('file','<strong>Error</strong><br />You must have to Upload File',2);
    return false;
  }
  /*if(!name.match(namechk)) {
    inlineMsg('name','<strong>Error</strong><br />You must enter a minimun 3 character name. NO numbers are allowed',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter a valid email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  
  if(contact == "") {
    inlineMsg('contact','<strong>Error</strong><br/>You must enter Your Contact number',2);
    return false;
  }  
  
   if(!contact.match(cno)) {
    inlineMsg('contact','<strong>Error</strong><br/>Please Enter your 10 digit mobile number in correct formate',2);
    return false;
  }  
  
    if(password == "") {
    inlineMsg('password','<strong>Error</strong><br/>You must enter a password.',2);
    return false;
  }
  
   if(!password.match(passwordchk)) {
    inlineMsg('password','<strong>Error</strong><br />enter 6 digit password, starting with a character and can contain only letters, numbers, underscores, or hyphens.',2);
    return false;
  }
  

   if(c_password != password) {
    inlineMsg('c_password','<strong>Error</strong><br/>Passwords did not match.',2);
    return false;
  }
   
	 

if(address == "") {
    inlineMsg('address','<strong>Error</strong><br/>You must enter Your Address',2);
    return false;
  }   

  
	if(city == "") {
    inlineMsg('city','<strong>Error</strong><br/>You must enter the name of City you live in.',2);
    return false;
  }
  
  if(!city.match(citychk)) {
    inlineMsg('city','<strong>Error</strong><br/>City name should be more than 2 characters and contains only aphabets.',2);
    return false;
  }
  
	if(state == "") {
    inlineMsg('state','<strong>Error</strong><br/>You must enter the name of State you live in.',2);
    return false;
  }
  
   if(!state.match(statechk)) {
    inlineMsg('state','<strong>Error</strong><br/>State name should be more than 2 characters and contains only aphabets.',2);
    return false;
  }

	if(country == "") {
    inlineMsg('country','<strong>Error</strong><br/>You must enter the name of Country you live in.',2);
    return false;
  }
  if(!country.match(countrychk)) {
    inlineMsg('country','<strong>Error</strong><br/>Contry name should be more than 2 characters and contains only aphabets.',2);
    return false;
  }
  
if(pincode == "") {
    inlineMsg('pincode','<strong>Error</strong><br/>You must enter the Pincode.',2);
    return false;
  }  
  
   if(!pincode.match(pinchk)) {
    inlineMsg('pincode','<strong>Error</strong><br/>Pincode should be 6 digits long.',2);
    return false;
  }
  
   if(captcha != answer) {
    inlineMsg('captcha','<strong>Error</strong><br/>Please provide the correct answer',2);
    return false;
  }*/
   

/*
  
  if(captcha_ans == "") {
    inlineMsg('captcha_ans','<strong>CaptchaError</strong><br/>You must enter the captcha value.',2);
    return false;
  }
  
  if(captcha_ans != var3) {
    inlineMsg('captcha_ans','<strong>Captcha Error</strong><br/>The captcha value entered is incorrect. Pls enter correct value.',2);
    return false;
  }


	
	 if(security_question == "") {
    inlineMsg('security_question','<strong>Error</strong><br />You must select your security question.',2);
    return false;
  }
  
  if(security_answer == "Security Answer") {
    inlineMsg('security_answer','<strong>Error</strong><br />You must an answer for the security question.');
    return false;
  }
*/	
  return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 100;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.png"; 
}